package com.ruoyi.system.service.impl;

import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.PublicOpinionEvents;
import com.ruoyi.system.domain.PublicOpinionTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.GatherTaskMapper;
import com.ruoyi.system.domain.GatherTask;
import com.ruoyi.system.service.IGatherTaskService;

/**
 * 采集任务Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-06
 */
@Service
public class GatherTaskServiceImpl implements IGatherTaskService {
    @Autowired
    private GatherTaskMapper gatherTaskMapper;

    /**
     * 查询采集任务
     *
     * @param id 采集任务ID
     * @return 采集任务
     */
    @Override
    public GatherTask selectGatherTaskById(Long id) {
        return gatherTaskMapper.selectGatherTaskById(id);
    }

    /**
     * 查询采集任务列表
     *
     * @param gatherTask 采集任务
     * @return 采集任务
     */
    @Override
    public List<GatherTask> selectGatherTaskList(GatherTask gatherTask) {
        return gatherTaskMapper.selectGatherTaskList(gatherTask);
    }

    /**
     * 根据事件生成采集任务
     *
     * @param event 事件
     * @return
     */
    @Override
    public int batchCreateGatherTask(PublicOpinionEvents event) {
        //事件开始日期
        Date startDate = event.getStartTime();
        //事件结束日期
        Date endDate = event.getEndTime();
        //取日期集合
        List<String> dateList = DateUtils.getDatesBetweenUsingJava7String(startDate, endDate);
        List<GatherTask> tasks = new ArrayList<>();
        for (String dateString : dateList) {
            GatherTask task = new GatherTask();
            task.setStartTime(DateUtils.getDateBegin(dateString));
            task.setEndTime(DateUtils.getDateEnd(dateString));
            task.setTitle(event.getEventTitle());
            task.setSearchArea(event.getSearchArea());
            task.setRelatedWords(event.getRelatedWords());
            task.setOpinionWords(event.getOpinionWords());
            task.setExclusionWords(event.getExclusionWords());
            task.setSourceType("event");
            task.setSourceId(event.getEventId());
            tasks.add(task);
        }
        return gatherTaskMapper.batchCreateGatherTask(tasks);
    }


    /**
     * 根据主题生成采集任务
     *
     * @param topic 主题
     * @return
     */
    @Override
    public int batchCreateGatherTask(PublicOpinionTopic topic) {
        //主题开始日期（设置为当天）
        Date startDate = DateUtils.getCurrentDate();
        //主题结束日期(未来一个月)
        // 获取当前日期
        LocalDate today = LocalDate.now();
        System.out.println("今天的日期: " + today);

        // 获取未来一个月的日期
        LocalDate nextMonth = today.plus(Period.ofMonths(1));
        System.out.println("未来一个月的日期: " + nextMonth);

        Date endDate = DateUtils.LocalDateToUdate(nextMonth);

        //取日期集合
        List<String> dateList = DateUtils.getDatesBetweenUsingJava7String(startDate, endDate);
        List<GatherTask> tasks = new ArrayList<>();
        for (String dateString : dateList) {
            GatherTask task = new GatherTask();
            task.setStartTime(DateUtils.getDateBegin(dateString));
            task.setEndTime(DateUtils.getDateEnd(dateString));
            task.setTitle(topic.getTitle());
            task.setSearchArea(topic.getSearchArea());
            task.setRelatedWords(topic.getRelatedWords());
            task.setOpinionWords(topic.getOpinionWords());
            task.setExclusionWords(topic.getExclusionWords());
            task.setSourceType("topic");
            task.setSourceId(topic.getId());
            tasks.add(task);
        }
        return gatherTaskMapper.batchCreateGatherTask(tasks);
    }

    /**
     * 根据主题修改采集任务
     *
     * @param event
     * @return
     */
    @Override
    public int batchUpdateGatherTask(PublicOpinionEvents event) {
        //事件开始日期
        Date startDate = event.getStartTime();
        //事件结束日期
        Date endDate = event.getEndTime();
        //取日期集合
        List<String> dateList = DateUtils.getDatesBetweenUsingJava7String(startDate, endDate);
        int success = 0;
        for (String dateString : dateList) {
            GatherTask task = new GatherTask();
            task.setStartTime(DateUtils.getDateBegin(dateString));
            task.setEndTime(DateUtils.getDateEnd(dateString));
            task.setTitle(event.getEventTitle());
            task.setSearchArea(event.getSearchArea());
            task.setRelatedWords(event.getRelatedWords());
            task.setOpinionWords(event.getOpinionWords());
            task.setExclusionWords(event.getExclusionWords());
            task.setSourceType("event");
            task.setSourceId(event.getEventId());

            GatherTask taskSearch = new GatherTask();
            taskSearch.setStartTime(DateUtils.getDateBegin(dateString));
            taskSearch.setEndTime(DateUtils.getDateEnd(dateString));
            taskSearch.setSearchArea(event.getSearchArea());
            taskSearch.setRelatedWords(event.getRelatedWords());
            taskSearch.setOpinionWords(event.getOpinionWords());
            taskSearch.setExclusionWords(event.getExclusionWords());
            List<GatherTask> tasks=gatherTaskMapper.selectGatherTaskList(taskSearch);
            if(tasks.size()==0){
                success += gatherTaskMapper.insertGatherTask(task);
            }

        }
        return success;
    }


    @Override
    public int batchUpdateGatherTask(PublicOpinionTopic topic) {
        //主题开始日期（设置为当天）
        Date startDate = DateUtils.getCurrentDate();
        //主题结束日期(未来一个月)
        // 获取当前日期
        LocalDate today = LocalDate.now();
        System.out.println("今天的日期: " + today);

        // 获取未来一个月的日期
        LocalDate nextMonth = today.plus(Period.ofMonths(1));
        System.out.println("未来一个月的日期: " + nextMonth);

        Date endDate = DateUtils.LocalDateToUdate(nextMonth);

        //取日期集合
        List<String> dateList = DateUtils.getDatesBetweenUsingJava7String(startDate, endDate);
        int success = 0;
        for (String dateString : dateList) {
            GatherTask task = new GatherTask();
            task.setStartTime(DateUtils.getDateBegin(dateString));
            task.setEndTime(DateUtils.getDateEnd(dateString));
            task.setTitle(topic.getTitle());
            task.setSearchArea(topic.getSearchArea());
            task.setRelatedWords(topic.getRelatedWords());
            task.setOpinionWords(topic.getOpinionWords());
            task.setExclusionWords(topic.getExclusionWords());
            task.setSourceType("topic");
            task.setSourceId(topic.getId());

            GatherTask taskSearch = new GatherTask();
            taskSearch.setStartTime(DateUtils.getDateBegin(dateString));
            taskSearch.setEndTime(DateUtils.getDateEnd(dateString));
            taskSearch.setSearchArea(topic.getSearchArea());
            taskSearch.setRelatedWords(topic.getRelatedWords());
            taskSearch.setOpinionWords(topic.getOpinionWords());
            taskSearch.setExclusionWords(topic.getExclusionWords());
            List<GatherTask> tasks=gatherTaskMapper.selectGatherTaskList(taskSearch);
            if(tasks.size()==0){
                success += gatherTaskMapper.insertGatherTask(task);
            }
        }
        return success;
    }


    /**
     * 新增采集任务
     *
     * @param gatherTask 采集任务
     * @return 结果
     */
    @Override
    public int insertGatherTask(GatherTask gatherTask) {
        return gatherTaskMapper.insertGatherTask(gatherTask);
    }


    @Override
    public int batchDeleteGatherTask(String type, Long[] ids) {
        return gatherTaskMapper.batchDeleteGatherTask(type, ids);
    }


    /**
     * 修改采集任务
     *
     * @param gatherTask 采集任务
     * @return 结果
     */
    @Override
    public int updateGatherTask(GatherTask gatherTask) {
        return gatherTaskMapper.updateGatherTask(gatherTask);
    }

    /**
     * 批量删除采集任务
     *
     * @param ids 需要删除的采集任务ID
     * @return 结果
     */
    @Override
    public int deleteGatherTaskByIds(Long[] ids) {
        return gatherTaskMapper.deleteGatherTaskByIds(ids);
    }

    /**
     * 删除采集任务信息
     *
     * @param id 采集任务ID
     * @return 结果
     */
    @Override
    public int deleteGatherTaskById(Long id) {
        return gatherTaskMapper.deleteGatherTaskById(id);
    }
}
